package com.second.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.second.common.pojo.R;
import com.second.order.dao.OrderRecordDao;
import com.second.order.feign.ClientFeign;
import com.second.order.pojo.entity.OrderCost;
import com.second.order.pojo.entity.OrderRecord;
import com.second.order.pojo.vo.OrderRecordQueryVo;
import com.second.order.pojo.vo.OrderRecordVo;
import com.second.order.pojo.vo.PageVo;
import com.second.order.pojo.vo.RecordAndCostVo;
import com.second.order.service.OrderCostService;
import com.second.order.service.OrderRecordService;
import com.second.order.util.PageData;
import com.second.order.util.PaymentMethod;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * (OrderRecord)表服务实现类
 *
 * @author 吕永奇
 * @since 2021-07-30 19:49:01
 */
@Service("orderRecordService")
public class OrderRecordServiceImpl extends ServiceImpl<OrderRecordDao, OrderRecord> implements OrderRecordService {

    /**
     * 订单记录dao层服务对象
     */
    @Resource
    private OrderRecordDao orderRecordDao;

    /**
     * 订单明细service层服务对象
     */
    @Resource
    private OrderCostService orderCostService;

    /**
     * 客户微服务调用接口
     */
    @Resource
    private ClientFeign clientFeign;


    /**
     * 查询所有的客户信息
     * @param pageVo 分页工具类
     * @param recordQueryVo 模糊查询工具类
     * @return 所有数据
     */
    @Override
    public R getAllOrderRecord(PageVo pageVo, OrderRecordQueryVo recordQueryVo) {
        // 开启分页
        Page<OrderRecord> page = new Page<>(pageVo.getCurrentPage(), pageVo.getPageSize());
        if (recordQueryVo != null) {
            // 截取实际取车时间数组，从中取出字符串
            if (!recordQueryVo.getActualPickTime().isEmpty()) {
                String pickStartTime = (String) recordQueryVo.getActualPickTime().get(0);
                String pickEndTime = (String) recordQueryVo.getActualPickTime().get(1);
                recordQueryVo.setPickStartTime(pickStartTime);
                recordQueryVo.setPickEndTime(pickEndTime);
            }
            // 截取实际还车时间数组，从中取出时间字符串
            if (!recordQueryVo.getActualReturnTime().isEmpty()) {
                String returnStartTime = (String) recordQueryVo.getActualReturnTime().get(0);
                String returnEndTime = (String) recordQueryVo.getActualReturnTime().get(1);
                recordQueryVo.setReturnStartTime(returnStartTime);
                recordQueryVo.setReturnEndTime(returnEndTime);
            }
        }
        // 根据条件模糊查询
        Page<OrderRecordVo> pageInfo = orderRecordDao.getAllOrderRecord(page,recordQueryVo);

        return PageData.returnPage(page,pageInfo);
    }

    /**
     * 获取订单的费用明细
     * @param costId 费用id
     * @return 费用明细数据
     */
    @Override
    public R getAllCostInfo(Integer costId) {
        OrderCost costInfo = orderRecordDao.getAllCostInfo(costId);
        if (costInfo != null) {
            return R.success().putObject(costInfo);
        }
        return R.error();
    }

    /**
     * 下订单，添加订单详情
     * @param orderRecordVo 订单详情
     * @return 下单结果
     */
    @Override
    @Transactional
    public R placeOrder(OrderRecordVo orderRecordVo) {
        OrderRecord orderRecord = new OrderRecord();
        // 使用UUID生成订单编号
        orderRecord.setOrderNumber(UUID.randomUUID().toString());
        // 设置订单状态为预约中
        orderRecord.setOrderStatus(1);
        // 设置租赁汽车类型
        orderRecord.setCarId(orderRecordVo.getCarId());
        // 设置客户id
        orderRecord.setClientId(orderRecordVo.getClientId());
        // 设置预约取车时间
        orderRecord.setSubscribePickTime(orderRecordVo.getSubscribePickTime());
        // 设置预约还车时间
        orderRecord.setSubscribeReturnTime(orderRecordVo.getSubscribeReturnTime());
        // 设置取车门店
        orderRecord.setPickStore(orderRecordVo.getPickStore());
        // 设置还车门店
        orderRecord.setReturnStore(orderRecordVo.getReturnStore());
        // 添加订单详情
        boolean save = this.save(orderRecord);
        // 设置订单详情的费用明细
        orderRecord.setOrderId(orderRecord.getOrderId());
        orderRecord.setCostId(orderRecord.getOrderId());
        boolean update = this.updateById(orderRecord);
        // 添加一条订单明细
        OrderCost orderCost = new OrderCost();
        // 设置费用明细的id与订单详情的主键id保持一致
        orderCost.setCostId(orderRecord.getOrderId());
        boolean b = orderCostService.save(orderCost);
        if (save == true && b == true && update == true) {
            // 返回新增数据的主键id和订单的编号
            ArrayList arrayList = new ArrayList();
            arrayList.add(orderRecord.getOrderId());
            arrayList.add(orderRecord.getOrderNumber());
            return R.success().putListData(arrayList);
        }
        return R.error();
    }

    /**
     * 支付订单
     * @param recordAndCostVo 订单详情和费用明细
     * @return 支付结果
     */
    @Override
    @Transactional
    public R paymentOrder(RecordAndCostVo recordAndCostVo) {
        // 判断支付方式是否为余额支付
        if (StringUtils.equals(recordAndCostVo.getPaymentMethod(),
                PaymentMethod.METHOD_BALANCE.getMethod())) {
            // 扣除会员的账户余额
            boolean b = clientFeign.deductAccountBalance(recordAndCostVo.getClientId(),
                    recordAndCostVo.getPaidAmount());
        }
        // 修改订单的状态为 已下单
        OrderRecord orderRecord = new OrderRecord();
        orderRecord.setOrderId(recordAndCostVo.getOrderId());
        orderRecord.setOrderStatus(2);
        // 设置订单详情表的费用id
        orderRecord.setCostId(recordAndCostVo.getOrderId());
        // 根据主键修改订单详情表的数据
        boolean u = this.updateById(orderRecord);
        // 根据费用id修改费用明细表中的数据
        boolean update = orderCostService.updateCost(recordAndCostVo);
        if (u == true && update == true) {
            return R.success();
        }
        return R.error();
    }

    /**
     * 修改订单中的实际取车时间
     * @param orderNumber 订单编号
     * @return 修改结果
     */
    @Override
    public R updateActualPickTime(String orderNumber) {
        boolean update = orderRecordDao.updateActualPickTime(orderNumber);
        if (update == true) {
            return R.success();
        }
        return R.error();
    }

    /**
     * 修改订单中的实际还车时间
     * @param orderNumber 订单编号
     * @return 修改结果
     */
    @Override
    public R updateActualReturnTime(String orderNumber) {
        boolean update = orderRecordDao.updateActualReturnTime(orderNumber);
        if (update == true) {
            return R.success();
        }
        return R.error();
    }

    /**
     * 根据会员id，查询所有的订单
     * @param clientId 会员id
     * @return 数据结果
     */
    @Override
    public R getOrderByClientId(Integer clientId) {
        if (clientId != null) {
            List<OrderRecord> orderRecordList = orderRecordDao.getOrderByClientId(clientId);
            if (orderRecordList.size() > 0) {
                return R.success().putListData(orderRecordList);
            }
        }
        return R.error();
    }

    /**
     * 根据订单编号，查询出订单的详细信息
     * @param orderId 订单编号
     * @return 数据结果
     */
    @Override
    public R getOrderInfoByOrderId(Integer orderId) {
        if (orderId != null) {
            OrderRecord orderRecord = orderRecordDao.getOrderInfoByOrderId(orderId);
            if (orderRecord != null) {
                return R.success().putObject(orderRecord);
            }
        }
        return R.error();
    }
}
